package org.fast.easy.system.remote.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import lombok.RequiredArgsConstructor;
import org.fast.easy.core.domain.messages.MessagePublishDomain;
import org.fast.easy.core.domain.result.R;
import org.fast.easy.framework.log.annotation.Log;
import org.fast.easy.framework.log.enums.BusinessType;
import org.fast.easy.framework.web.core.BaseController;
import org.fast.easy.messages.service.IMessageService;
import org.fast.easy.system.api.SysNoticeApi;
import org.fast.easy.system.notice.domain.bo.SysNoticeBo;
import org.fast.easy.system.upms.business.ISysNoticeBusiness;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 公告 信息操作处理
 *
 * @author Lion Li
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/system/notice")
public class RemoteSysNoticeController extends BaseController implements SysNoticeApi {
    private final ISysNoticeBusiness sysNoticeBusiness;
    private final IMessageService messageService;

    /**
     * 新增通知公告
     */
    @Override
    @SaCheckPermission("system:notice:add")
    @Log(title = "通知公告", businessType = BusinessType.INSERT)
    @PostMapping
    public R<Void> add(@Validated @RequestBody SysNoticeBo notice) {
        MessagePublishDomain message = sysNoticeBusiness.add(notice);
        if (message!=null) {
            if(messageService.publishAll(message)){
                return R.ok();
            }
            return R.fail();
        }
        return R.fail();
    }
}
